// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.folder;

import java.io.File;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.warehouse.i18n.Messages;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.wizard.Wizard;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class NewFolderWizard extends Wizard {

    private NewFolderWizardPage page;

    private IPath path;

    private final ENodeCategoryName nodeCategoryName;

    private String origname;

    public NewFolderWizard(ENodeCategoryName nodeCategoryName, IPath path) {
        super();
        this.path = path;
        this.nodeCategoryName = nodeCategoryName;
        setWindowTitle(Messages.getString("CreateFolderAction.createFolder")); //$NON-NLS-1$
        setDefaultPageImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.WAREHOUSE_WIZARD_FOLDER));
    }

    public NewFolderWizard(ENodeCategoryName nodeCategoryName, IPath path, String origname) {
        this(nodeCategoryName, path);
        this.origname = origname;
        setWindowTitle(Messages.getString("NewFolderWizard.editPro")); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#addPages()
     */
    @Override
    public void addPages() {
        if (origname != null) {
            page = new NewFolderWizardPage(nodeCategoryName, path, origname);
        } else {
            page = new NewFolderWizardPage(nodeCategoryName, path);
        }
        addPage(page);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#performFinish()
     */
    @Override
    public boolean performFinish() {
        String folderName = page.getFolderName();
        boolean create = origname == null;
        IFolder folder = null;
        if (create) {
            path = path.append(folderName);
            folder = WarehouseResourceFactory.getFolder(nodeCategoryName, path);
            File file = folder.getLocation().toFile();
            file.mkdirs();
        } else {
            folder = WarehouseResourceFactory.getFolder(nodeCategoryName, path);
            File file = ((IFolder) folder.getParent()).getFolder(folderName).getLocation().toFile();
            folder.getLocation().toFile().renameTo(file);
        }
        try {
            folder.getParent().refreshLocal(IResource.DEPTH_INFINITE, null);
        } catch (CoreException ex) {
            ex.printStackTrace();
        }

        return true;
    }

}
